import java.util.Stack;

class Solution2 {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (!stack.isEmpty() && stack.peek() == (s.charAt(i))) {
                stack.pop();
            } else {
                stack.push(s.charAt(i));
            }
        }
        StringBuffer ret = new StringBuffer();
        while(!stack.isEmpty()){
            ret.append(stack.pop());
        }
        return ret.reverse().toString();
    }
}